﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using QuanLyDeTaiKHCN.Obj_class;
namespace QuanLyDeTaiKHCN.UserControls.Others
{
    public partial class PhanCongDeTai : System.Web.UI.UserControl
    {
        public SecurityLib.Sec_AES _ps = new SecurityLib.Sec_AES();
        _dbDataContext _db = new _dbDataContext();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindGrid();
                inputmadetai.Value = "";
            }
        }
        protected void BindGrid()
        {
            var list = from p in _db.tblDanhSachDeTaiDuAns
                       select new
                       {
                           MaDeTaiDuAn = p.MaDeTaiDuAn.Trim(),
                           TenDeTaiDuAn = p.TenDeTaiDuAn.Trim(),
                           NhanVienThuLy = p.tblAcount.TenHienThi.Trim(),
                           Ngay = string.Format("{0:dd/MM/yyyy}", p.NgayKhoiTaoDeTai)
                       };
            grid.DataSource = list.ToList().OrderByDescending(r => r.Ngay);
            grid.DataBind();
        }

        protected void grid_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            pnlist.Visible = false;
            pnphancong.Visible = true;
            if (drlNguoiThuLy.Items.Count <= 1)
            {
                var list = _db.tblAcounts.Where(r => r.ID != 1).ToList();
                ListItem items = new ListItem("---Chọn---", "-1");
                drlNguoiThuLy.Items.Add(items);
                foreach (var item in list)
                {
                    items = new ListItem(item.TenHienThi.Trim(), item.ID.ToString());
                    drlNguoiThuLy.Items.Add(items);
                }
            }
            inputmadetai.Value = grid.DataKeys[e.RowIndex].Value.ToString().Trim();
            lblTenDeTai.Text = "<u>Tên đề tài:</u> "+((Label)grid.Rows[e.RowIndex].FindControl("lblTenDeTaiDuAn")).Text;
            lblstatus.Text = "";
        }

        protected void btnsave_Click(object sender, EventArgs e)
        {
            if (inputmadetai.Value != "")
            {
                var item = _db.tblDanhSachDeTaiDuAns.Single(r => r.MaDeTaiDuAn.Trim() == inputmadetai.Value.Trim());
                item.NguoiQuanLy = int.Parse(drlNguoiThuLy.SelectedValue.ToString());
                try
                {
                    _db.SubmitChanges();
                    lblstatus.Text = "Đã lưu.";
                    BindGrid();
                }
                catch { lblstatus.Text = "Không lưu được."; }
            }
        }

        protected void LinkButton2_Click(object sender, EventArgs e)
        {
            pnlist.Visible = true;
            pnphancong.Visible = false;
            inputmadetai.Value = "";
        }

    }
}